package com.itheima.pinda.web.scope;


import com.itheima.pinda.DTO.angency.AgencyScopeDto;
import com.itheima.pinda.DTO.user.CourierScopeDto;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.mapstruct.AgencyDTO2Entity;
import com.itheima.pinda.service.PdAgencyScopeService;
import com.itheima.pinda.service.PdCourierScopeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 业务范围表
 *
 * @author llhuang10
 * @date 2021/10/3 22:25
 */
@Log
@Slf4j
@Validated
@RestController
@Api(tags = "业务范围表")
@RequestMapping("/scope")
public class ScopeController {

    @Resource
    private AgencyDTO2Entity agencyDTO2Entity;

    @Resource
    private PdAgencyScopeService agencyScopeService;

    @Resource
    private PdCourierScopeService courierScopeService;


    /**
     * 批量保存机构业务范围
     *
     * @param dtoList 机构业务范围信息
     * @return 返回信息
     */
    @PostMapping("/agency/batch")
    @ApiOperation("批量保存机构业务范围")
    public Result batchSaveAgencyScope(@RequestBody List<AgencyScopeDto> dtoList) {
        boolean res = agencyScopeService.batchSaveAgencyScope(dtoList);
        return res ? Result.ok() : Result.error("保存失败, 请联系管理员");
    }

    /**
     * 删除机构业务范围信息
     *
     * @param dto 参数
     * @return 返回信息
     */
    @DeleteMapping("/agency")
    @ApiOperation("删除机构业务范围信息")
    public Result deleteAgencyScope(@Valid @RequestBody AgencyScopeDto dto) {
        boolean res = agencyScopeService.deleteAgencyScope(dto);
        return res ? Result.ok() : Result.error("删除失败, 请联系管理员");
    }

    /**
     * 获取机构业务范围列表
     *
     * @param areaId   行政区域id
     * @param agencyId 机构id
     * @return 机构业务范围列表
     */
    @GetMapping("/agency")
    @ApiOperation("获取机构业务范围列表")
    public List<AgencyScopeDto> findAllAgencyScope(@RequestParam(name = "areaId", required = false) String areaId,
                                                   @RequestParam(name = "agencyId", required = false) String agencyId,
                                                   @RequestParam(name = "agencyIds", required = false) List<String> agencyIds,
                                                   @RequestParam(name = "areaIds", required = false) List<String> areaIds) {
        return agencyScopeService.findAll(areaId, agencyId, agencyIds, areaIds);
    }

    /**
     * 批量保存快递员业务范围
     *
     * @param dtoList 快递员业务范围信息
     * @return 返回信息
     */
    @PostMapping("/courier/batch")
    @ApiOperation("批量保存快递员业务范围")
    public Result batchSaveCourierScope(@RequestBody List<CourierScopeDto> dtoList) {
        boolean res = courierScopeService.batchSaveCourierScope(dtoList);
        return res ? Result.ok() : Result.error("保存失败, 请联系管理员");
    }

    /**
     * 删除快递员业务范围信息
     *
     * @param dto 参数
     * @return 返回信息
     */
    @DeleteMapping("/courier")
    @ApiOperation("删除快递员业务范围信息")
    public Result deleteCourierScope(@Valid @RequestBody CourierScopeDto dto) {
        boolean res = courierScopeService.deleteCourierScope(dto);
        return res ? Result.ok() : Result.error("删除失败, 请联系管理员");
    }


    /**
     * 获取快递员业务范围列表
     *
     * @param areaId 行政区域id
     * @param userId 快递员id
     * @return 快递员业务范围列表
     */
    @GetMapping("/courier")
    @ApiOperation("获取快递员业务范围列表")
    public List<CourierScopeDto> findAllCourierScope(@RequestParam(name = "areaId", required = false) String areaId,
                                                     @RequestParam(name = "userId", required = false) String userId) {
        return courierScopeService.findAllCourierScope(areaId, userId);
    }
}
